package com.itpeng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itpeng.constant.MessageConstant;
import com.itpeng.entity.PageResult;
import com.itpeng.entity.QueryPageBean;
import com.itpeng.entity.Result;
import com.itpeng.exception.HealthException;
import com.itpeng.pojo.CheckItem;
import com.itpeng.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Aythor:彭彦钦
 * @Data: 2020/9/18  20:58
 */
@RestController
@RequestMapping("/checkItem")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    /**
     * 查询所有的检查项
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){

        List<CheckItem> checkItems = checkItemService.findAll();

        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItems);

    }

    /**
     * 新增检查项
     * @param checkItem
     * @return
     */
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    public Result add(@RequestBody CheckItem checkItem){

        checkItemService.add(checkItem);
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);

    }

    /**
     * 根据条件分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){

        PageResult page = checkItemService.findPage(queryPageBean);

        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,page);
    }

    /**
     * 根据ID删除数据
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    public Result deleteById(Integer id) throws HealthException {

        checkItemService.deleteById(id);

        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id){

        CheckItem checkItem = checkItemService.findById(id);

        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
    }

    /**
     * 修改数据
     * @param checkItem
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody CheckItem checkItem){

        checkItemService.update(checkItem);

        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

}
